package edu.snake.implement.controller.character;

import edu.snake.core.controller.ArenaController;
import edu.snake.core.controller.ArenaController.Direction;
import edu.snake.core.controller.CharacterController;
import edu.snake.core.controller.GameEngine;
import edu.snake.core.controller.command.Algorithm;
import edu.snake.core.model.Character;
import edu.snake.implement.command.ListAlgorithm;


public class BaseCharacterController implements CharacterController {

    public Thread t;
    protected volatile boolean running;

    protected Character character;
    protected ArenaController arenaController;
    protected GameEngine gameEngine;
    protected ListAlgorithm algorithm;

    protected Direction direction = Direction.LEFT;


    public BaseCharacterController(Character character, ArenaController arenaController, GameEngine gameEngine) {
        this.character = character;
        this.arenaController = arenaController;
        this.gameEngine = gameEngine;
        this.algorithm = new ListAlgorithm();
    }

    @Override
    public Character getModel() {
        return character;
    }

    @Override
    public ArenaController getArenaController() {
        return arenaController;
    }

    @Override
    public GameEngine getGameEngine() {
        return gameEngine;
    }

    @Override
    public void add(Algorithm algorithm) {
        this.algorithm.add(algorithm);
    }

    @Override
    public Direction getDirection() {
        return direction;
    }

    @Override
    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    //region Thread control methods
    @Override
    public void start() {
        running = true;
        if (t == null) {
            t = new Thread(this);
            t.setName("Thread".concat("  ").concat(getModel().toString()));
            t.start();
        }

    }

    @Override
    public void run() {

        while (running) {
            algorithm.action();
        }
    }


    @Override
    public void stop() {
        running = false;
        t = null;
    }


    @Override
    public boolean isRunning() {
        return running;
    }
    //endregion


}
